package com.fw.ui.view.common.event;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.ErrorBean;
import com.fw.service.bouser.bean.BOUserBean;
import com.fw.ui.ApplicationUI;
import com.fw.ui.bao.BOUserBAO;
import com.fw.ui.bao.MessageBAO;
import com.fw.ui.util.SessionInfoUtils;
import com.fw.ui.util.UICommonUtils;
import com.fw.ui.util.VaadinApplicationObjectSupport;
import com.fw.ui.view.common.ChangePasswordPanel;
import com.fw.ui.view.event.AbstractButtonClickListener;
import com.vaadin.ui.Button.ClickEvent;

@Component
@Scope(SCOPE_PROTOTYPE)
public class ChangePasswordPanelSaveButtonClickListener extends AbstractButtonClickListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    protected transient VaadinApplicationObjectSupport app;
    @Autowired
    private UICommonUtils uiCommonUtils;
    @Autowired
    private MessageBAO messageBAO;
    @Autowired
    private BOUserBAO boUserBAO;
    @Autowired
    private SessionInfoUtils sessionInfoUtils;

    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> click(ClickEvent event) throws Exception {
	ChangePasswordPanel changePasswordPanel = (ChangePasswordPanel) this.getBasedComponent();

	BOUserBean boUserBean = boUserBAO.getBOUser(changePasswordPanel.getUserId());

	String oldPasswordValue = changePasswordPanel.getTxtOldPassword().getValue();
	String newPasswordValue = changePasswordPanel.getTxtNewPassword().getValue();

	boUserBean.setOldPassword(oldPasswordValue);
	boUserBean.setNewPassword(newPasswordValue);

	ApplicationUI applicationUI = (ApplicationUI) changePasswordPanel.getUI();
	sessionInfoUtils.setSessionInfoDetails(boUserBean, applicationUI.getAuthenticationBean(), applicationUI.getLocale());

	Map<String, Serializable> mapResult = this.boUserBAO.changePasswordBOUser(boUserBean);
	Map<String, Object> result = new HashMap<String, Object>();
	if (mapResult != null) {
	    List<ErrorBean> errorBeans = (List<ErrorBean>) mapResult.get(List.class.getName() + "." + ErrorBean.class.getName());
	    BOUserBean userBean = (BOUserBean) mapResult.get(BOUserBean.class.getName());
	    if (errorBeans != null && !errorBeans.isEmpty()) {
		result.put(MessageBAO.MESSAGE_WARNING_TYPE, errorBeans);
	    } else {
		if (userBean != null && userBean.getId() != null) {
		    if (changePasswordPanel.isRequireNewLogin()){
			applicationUI.buildLoginView(true);
		    } else if (changePasswordPanel.getNavigatePath() != null){
			String navigationState = changePasswordPanel.getNavigatePath();
			applicationUI.getNavigator().navigateTo(navigationState);
		    }
		    uiCommonUtils.clearAllUIComponentWithPrefixDefaultValue(changePasswordPanel, null);
		    result.put(MessageBAO.MESSAGE_INFO_TYPE, "com.fw.ui.view.common.ChangePasswordPanel.message.changepassword");
		} else {
		    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "com.fw.ui.view.common.ChangePasswordPanel.message.changepassword.failed");
		}
	    }
	} else {
	    result.put(MessageBAO.MESSAGE_WARNING_TYPE, "message.error.common");
	}
	return result;
    }

    @Override
    public boolean validate(ClickEvent event) throws Exception {
	try {
	    ChangePasswordPanel changePasswordPanel = (ChangePasswordPanel) this.getBasedComponent();
	    uiCommonUtils.clearAllUIComponentNoInputFieldWithPrefix(changePasswordPanel);

	    String oldPasswordValue = changePasswordPanel.getTxtOldPassword().getValue();
	    String newPasswordValue = changePasswordPanel.getTxtNewPassword().getValue();
	    String confrimNewPasswordValue = changePasswordPanel.getTxtConfirmNewPassword().getValue();

	    boolean oldPasswordRequireValid = (oldPasswordValue != null && oldPasswordValue.length() > 0);
	    boolean newPasswordRequireValid = (newPasswordValue != null && newPasswordValue.length() > 0);
	    boolean confirmNewPasswordRequireValid = (confrimNewPasswordValue != null && confrimNewPasswordValue.length() > 0);

	    boolean isOldPasswordEqual = false;
	    boolean isConfirmNewPasswordEqual = false;

	    if (!oldPasswordRequireValid) {
		changePasswordPanel.getTxtOldPassword().addStyleName("v-textfield-error");
		changePasswordPanel.getLblOldPasswordValidateMessage().setValue(
			this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.message.oldpassword.mandatory"));
		changePasswordPanel.getRow1Validate().setVisible(true);
	    } else {
		String oldPassword = changePasswordPanel.getOldPassword();
		if (!oldPassword.equals(oldPasswordValue)) {
		    isOldPasswordEqual = false;

		    changePasswordPanel.getTxtOldPassword().addStyleName("v-textfield-error");
		    changePasswordPanel.getLblOldPasswordValidateMessage().setValue(
			    this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.message.oldpassword.notequal"));
		    changePasswordPanel.getRow1Validate().setVisible(true);

		} else {
		    isOldPasswordEqual = true;
		}
	    }

	    if (!newPasswordRequireValid) {
		changePasswordPanel.getTxtNewPassword().addStyleName("v-textfield-error");
		changePasswordPanel.getLblNewPasswordValidateMessage().setValue(
			this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.message.newpassword.mandatory"));
		changePasswordPanel.getRow2Validate().setVisible(true);
	    }

	    if (!confirmNewPasswordRequireValid) {
		changePasswordPanel.getTxtConfirmNewPassword().addStyleName("v-textfield-error");
		changePasswordPanel.getLblConfirmNewPasswordValidateMessage().setValue(
			this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.message.confirmnewpassword.mandatory"));
		changePasswordPanel.getRow5Validate().setVisible(true);
	    } else {
		if (!newPasswordValue.equals(confrimNewPasswordValue)) {
		    isConfirmNewPasswordEqual = false;

		    changePasswordPanel.getTxtConfirmNewPassword().addStyleName("v-textfield-error");
		    changePasswordPanel.getLblConfirmNewPasswordValidateMessage().setValue(
			    this.messageBAO.getMessage("com.fw.ui.view.common.ChangePasswordPanel.message.confirmnewpassword.notequal"));
		    changePasswordPanel.getRow5Validate().setVisible(true);

		} else {
		    isConfirmNewPasswordEqual = true;
		}
	    }

	    if (isConfirmNewPasswordEqual && isOldPasswordEqual && oldPasswordRequireValid && newPasswordRequireValid
		    && confirmNewPasswordRequireValid) {
		return true;
	    } else {
		return false;
	    }

	} catch (Exception e) {
	    e.printStackTrace();
	    return false;
	}
    }

}
